Ext.define('CS.panel.BasePanel', {
	extend: 'CS.config.BaseConfig',
	requires: ['CS.plugin.PanelDataDeal', 'CS.plugin.PanelDomDeal'],

	transform: {
		autoShow: 'show',
		bottomAlign: 'bAlign',
		clsName: 'fieldClassName',
		closeAction: 'closeType',
		itemId: 'panelId'
	},

	defaultConfig: {
		autoHeight: true,
		autoRender: true,
		border: false,
		bottomAlign: 'center',
		clsName: '',
		isAsync: false,
		paging: true,
		pageSize: 10
	},

	defaultPlugins: ['paneldatadeal', 'paneldomdeal'],

	initConfig: function(config) {
		var me = this;

		if (CS.isNull(config.xtype)) {
			config.xtype = 'panel.' + config.panelType;
		}

		me.initItems(config);
		me.initButtons(config);
	},

	initItems: function(config) {
		config.items = config.items || [];

		if (CS.isNotEmpty(config.subFieldList)) {
			Ext.each(config.subFieldList, function(fieldConfig) {
				config.items.push(Ext.create('CS.field.BaseField', fieldConfig).getConfig());
			});
		} else if (CS.isNotEmpty(config.subPanelList)) {
			Ext.each(config.subPanelList, function(panelConfig) {
				config.items.push(Ext.create('CS.panel.BasePanel', panelConfig).getConfig());
			});
		}

		delete config.subFieldList;
		delete config.subPanelList;
	},

	initButtons: function(config) {
		if (CS.isNotEmpty(config.buttonList)) {
			var tbar = {
				xtype: 'toolbar',
				dock: 'top',
				items: []
			};

			var bbar = {
				xtype: 'toolbar',
				dock: 'bottom',
				ui: 'footer',
				layout: {
					pack: config.bottomAlign
				},
				items: []
			};

			Ext.each(config.buttonList, function(button, index) {
				var item = {
					itemId: button.buttonId,
					text: button.text
				};

				if (CS.isNotEmpty(button.icon)) {
					item.iconCls = 'cs-icon-' + button.icon;
				}

				switch (button.buttonSite) {
				case 't':
					if (CS.isNotEmpty(tbar.items)) {
						tbar.items.push('-');
					}
					tbar.items.push(item);
					break;
				case 'b':
					if (index > 0) {
						bbar.items.push({
							xtype: 'tbspacer',
							width: 20
						});
					} else if (config.bottomAlign == 'right') {
						bbar.items.push('->');
					}

					bbar.items.push(item);
					break;
				}
			});

			config.dockedItems = [];
			if (CS.isNotEmpty(tbar.items)) {
				config.dockedItems.push(tbar);
			}

			if (CS.isNotEmpty(bbar.items)) {
				config.dockedItems.push(bbar);
			}
		}
	}

});
